package com.ssb.plugins;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.alibaba.fastjson.JSON;
import com.apache.cache.service.impl.LoadCacheFactory;
import com.apache.cache.util.Validator;
import com.apache.database.datasource.ConnctionData;
import com.apache.database.datasource.DynamicDataSource;
import com.apache.exception.BusinessException;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.ius.plugin.IusPluginUtil;
import com.apache.rpc.entity.RpcDatasource;
import com.apache.tools.StrUtil;
import com.ssb.util.AESUtil;
import com.ssb.util.SendHttpUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * 黄河接口对接-抽像父类
 */
public abstract class HheSuperPlugin implements CustomMethodPlugin {

    protected Logger log = Logger.getLogger("huanghe logs");
    protected  final static String DATA_SOURCE = DynamicDataSource.getInstance().getDefaultDataSourceKey();
    //投保单：http://223.71.98.124:8002/sp/resources/Externalcommon/proEntranceSSK
    //获取流水号接口：http://223.71.98.124:8002/sp/resources/Externalcommon/tradeNoCommon
    //电子保单/电子发票/批改：http://223.71.98.124:8002/sp/resources/Externalcommon/surrenderSSK
    private final String aesKey= "ABQDKEY";
    //自动注入redis配置属性文件
    @Autowired
    private Environment env;
    /**
     * 发送接口请求
     * @param map
     * @return
     */
    protected Object sendInterface(String urlSufix,Map<String, Object> map,Map<String,String> heads){
        String url = env.getProperty("huanghe_risk_url") + urlSufix;
        String result = SendHttpUtil.sendHttpStr(url,"post", map, heads, aesKey);
        if(Validator.isNotNull(result)){
            String str = AESUtil.decrypt(result,aesKey);
            return str;
        }
        return null;
    }

    /**
     * 发送接口请求
     */
    protected Object sendInterface(String urlSufix, List params, Map<String,String> heads){
        String url = env.getProperty("huanghe_risk_url") +  urlSufix;
        String result = SendHttpUtil.sendHttpStr(url,"post", params, heads, aesKey);
        if(Validator.isNotNull(result)){
            String str = AESUtil.decrypt(result,aesKey);
            return str;
        }
        return null;
    }

    /**
     * 根据ID获取订单列表
     */
    protected Object getOrderById(String infoId){
        StringBuilder sb = new StringBuilder();
        sb.append("select info_id, channel_id,request_no,product_id,policy_no,premium,applicant_name,applicant_certi_no,applicant_type,invoice_no,policy_down_url");
        sb.append(" from ss_insur_order where info_id='"+infoId+"'");
        List list = (List) select(sb.toString(),DATA_SOURCE);
        if(!Validator.isEmpty(list)){
           return list.get(0);
        }
        return null;
    }

    public List select(String sql, String datasource) throws BusinessException {
        return (List)exeDbSelect(datasource,sql,"list");
    }

    public Map selectInfo(String sql, String datasource) throws BusinessException {
        return (Map)exeDbSelect(datasource,sql,"obj");
    }

    private Object exeDbSelect(String datasource,String sql, String type){
        DruidDataSource dbDao = null;
        if(StrUtil.isNull(datasource)){
            datasource = DynamicDataSource.getInstance().getDefaultDataSourceKey();
        }
        Object obj;
        if (!DynamicDataSource.getInstance().checkDataSource(datasource)) {
            obj = LoadCacheFactory.getInstance().getCacheManager("").getCacheObjectByKey("rpc_datasource_" + datasource);
            if (null == obj) {
                throw new BusinessException("获取数据源[" + datasource + "]失败！");
            }
            RpcDatasource rds = (RpcDatasource)obj;
            if (!DynamicDataSource.getInstance().addDataSource(rds.getSysEname(), rds.getDriverClassName(), rds.getJdbcUrl(), rds.getJdbcUsername(), rds.getJdbcPassword())) {
                throw new BusinessException("连接数据源[" + datasource + "]失败！");
            }
        }
        try {
            dbDao = DynamicDataSource.getInstance().getDataSource(datasource);
            if("obj".equals(type)) {
                return ConnctionData.getInstance().selectInfo(dbDao.getConnection(),sql);
            }else{
                return ConnctionData.getInstance().selectListInfo(dbDao.getConnection(), sql);
            }
        } catch (SQLException var6) {
            DynamicDataSource.getInstance().removeDataSource(datasource);
        }
        return null;
    }

    @Override
    public Object doInvoke(Map<String, Object> map) {
        return null;
    }
}
